package com.yfp.goods.controller;

import cn.hutool.core.bean.BeanUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.goods.domain.Goods;
import com.yfp.goods.domain.dto.GoodsDto;
import com.yfp.goods.domain.dto.GoodsInfoDto;
import com.yfp.goods.domain.vo.GoodsExportVO;
import com.yfp.goods.domain.vo.GoodsUpVo;
import com.yfp.goods.domain.vo.GoodsVo;
import com.yfp.goods.service.GoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 商品表(Goods)表控制层
 *
 * 
 * @since 2023-12-20 22:20:53
 */
@Tag(name = "配菜信息")
@RestController
@RequestMapping("goods")
public class GoodsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询
     *
     * @param goods       筛选条件
     * @return 查询结果
     */
    @Operation(summary = "分页查询所有数据")
    @GetMapping
    public TableDataInfo queryByPage(Goods goods, @Valid Integer pageNum, @Valid Integer pageSize) {
        return getDatableByStream(this.goodsService.queryAll(goods), pageNum, pageSize);
    }

    @PostMapping("/export")
    @Operation(summary = "配菜信息导出")
    public void export(HttpServletResponse response, Goods goods){
        List<GoodsDto> goodsDtos = goodsService.queryAll(goods);
        List<GoodsExportVO> goodsExportVOS = BeanUtil.copyToList(goodsDtos, GoodsExportVO.class);

        ExcelUtil<GoodsExportVO> util = new ExcelUtil<>(GoodsExportVO.class);
        util.exportExcel(response, goodsExportVOS, "配菜信息");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @GetMapping("{id}")
    public R<GoodsInfoDto> queryById(@PathVariable("id") String id) {
        return R.ok(this.goodsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param goodsVo 实体
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @PostMapping
    @Log(title = "新增商品", businessType = BusinessType.INSERT)
    public R<Goods> add(@RequestBody @Valid GoodsVo goodsVo) {
        return R.ok(this.goodsService.insert(goodsVo));
    }

    /**
     * 编辑数据
     *
     * @param goodsUpVo 实体
     * @return 编辑结果
     */
    @Operation(summary = "编辑数据")
    @PostMapping("update")
    @Log(title = "编辑商品", businessType = BusinessType.UPDATE)
    public R<GoodsUpVo> updateGoodsInfo(@Valid @RequestBody GoodsUpVo goodsUpVo) {
        return R.ok(this.goodsService.updateGoodsInfo(goodsUpVo));
    }


    @Operation(summary = "禁用/启用商品")
    @GetMapping("dis/{goodsId}")
    @Log(title = "禁用商品", businessType = BusinessType.UPDATE)
    public R disableGoods(@PathVariable("goodsId") @Valid String goodsId) {
        return this.goodsService.disableGoods(goodsId) ? R.ok() : R.fail();
    }

}

